package com.hex.liyue.usermodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.usermodule.bean.MyOrderResultBean;

import java.util.List;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterOrder extends RecyclerView.Adapter<AdapterOrder.MyHolder> {
    private OnItemClickListener onItemClickListener;
    private List<MyOrderResultBean.DataBean> list;

    public void setList(List<MyOrderResultBean.DataBean> list) {
        this.list = list;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int position);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_order, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        MyOrderResultBean.DataBean dataBean = list.get(position);
        holder.rl_item_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClickListener(position);
            }
        });

        holder.tv_order_time.setText("下单时间：" + DateUtils.getFormatDate(dataBean.create_time * 1000, "yyyy-MM-dd HH:mm:ss"));
        //订单状态 -1. 取消 1. 未支付 2. 已支付 3. 过期
        if (dataBean.status.equals("-1")) {
            holder.tv_order_state.setText("订单状态：已取消");
        } else if (dataBean.status.equals("1")) {
            holder.tv_order_state.setText("订单状态：未支付");
        } else if (dataBean.status.equals("2")) {
            holder.tv_order_state.setText("订单状态：已支付");
        } else if (dataBean.status.equals("3")) {
            holder.tv_order_state.setText("订单状态：已过期");
        }
        holder.tv_order_id.setText("订单编号：" + dataBean.order_sn);
        holder.tv_order_price.setText("订单金额：" + dataBean.pay_money);
        Glide.with(LiYueApplication.getInstance())
                .load(dataBean.gift.gift_url)
                .error(R.drawable.no_icon)
                .into(holder.iv_icon);
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_item_box;
        TextView tv_order_time;
        TextView tv_order_state;
        TextView tv_order_id;
        TextView tv_order_price;
        ImageView iv_icon;

        public MyHolder(View itemView) {
            super(itemView);
            rl_item_box = (RelativeLayout) itemView.findViewById(R.id.rl_item_box);
            tv_order_time = (TextView) itemView.findViewById(R.id.tv_order_time);
            tv_order_state = (TextView) itemView.findViewById(R.id.tv_order_state);
            tv_order_id = (TextView) itemView.findViewById(R.id.tv_order_id);
            tv_order_price = (TextView) itemView.findViewById(R.id.tv_order_price);
            iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
        }
    }
}
